package it.openHWHackaton.arduinoTableSoccer;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;

public class Serial implements SerialPortEventListener {
	SerialPort serialPort;

	private static final String PORT_NAMES[] = {
		"/dev/cu.usbmodemfa131",//Mac Galileo
		"/dev/cu.usbmodemfd121",//Mac Galileo
		"/dev/cu.usbmodem411",//Mac Galileo
		"/dev/cu.usbmodem621",//Mac Galileo
		"/dev/ttyUSB0", // Linux
		"COM3", // Windows
	};

	private BufferedReader input;
	private static OutputStream output;
	private static final int TIME_OUT = 2000;
	private static final int DATA_RATE = 9600;
	private RATSGame game=null;



	public void initialize() {
		CommPortIdentifier portId = null;
		Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

		while (portEnum.hasMoreElements()) {
			CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
			for (String portName : PORT_NAMES) {
				if (currPortId.getName().equals(portName)) {
					portId = currPortId;
					break;
				}
			}
		}
		if (portId == null) {
			System.out.println("Could not find COM port.");
			return;
		}

		try {
			serialPort = (SerialPort) portId.open(this.getClass().getName(),TIME_OUT);

			serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,    SerialPort.PARITY_NONE);

			input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
			output = serialPort.getOutputStream();

			serialPort.addEventListener(this);
			serialPort.notifyOnDataAvailable(true);
		}
		catch (Exception e) {
			System.err.println(e.toString());
		}
	}

	public synchronized void serialEvent(SerialPortEvent oEvent) {
		if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
			try {
				String inputLine=input.readLine();
				System.out.println(inputLine);
				if(game!=null){
					//start
					if(inputLine.equals("0")){
						game.resetMatch();
					}
					//start
					if(inputLine.equals("1")){
						game.startMatch();
					}
					//pause
					if(inputLine.equals("2")){
						game.pauseMatch();
					}
					//resume
					if(inputLine.equals("3")){
						game.resumeMatch();
					}
					//goal1
					if(inputLine.equals("4")){
						game.goal1();
					}
					//goal2
					if(inputLine.equals("5")){
						game.goal2();
					}
					//stupidsound1
					if(inputLine.equals("6")){
						game.soundStupid1();
					}
					//stupidsound2
					if(inputLine.equals("7")){
						game.soundStupid2();
					}
					if(inputLine.equals("8")){
						game.winner1();
					}
					if(inputLine.equals("9")){
						game.winner2();
					}
				}
			} catch (Exception e) {
				System.err.println(e.toString());
			}
		}
	}

	public synchronized void close() {
		if (serialPort != null) {
			serialPort.removeEventListener();
			serialPort.close();
		}
	}

	public Serial(String ncom){
		if(Integer.parseInt(ncom)>=3 && Integer.parseInt(ncom)<=9)
			PORT_NAMES[3] = "COM" + ncom;
		initialize();
		Thread t=new Thread() {
			public void run() {
				try {Thread.sleep(1000000);} catch (InterruptedException ie) {}
			}
		};
		t.start();
		System.out.println("Serial Comms Started");
	}

	public synchronized void send(int b){
		try{
			output.write(b);
		}
		catch (Exception e) {
			System.err.println(e.toString());
		}
	}
	public void setGame(RATSGame game){
		this.game=game;
	}
	public synchronized int read(){
		int b = 0;

		try{
			b = (int)input.read();
		}
		catch (Exception e) {
			System.err.println(e.toString());
		}
		return b;
	}
}